<?php
namespace AuctionCore\Spm;
/**
 * 车源列表
 * @author       shijiaibing
 */
class AccountManage extends \AuctionSpm\Controller\BaseController {
    //初始化
    public function __construct() {           
        parent::__construct();
    }
	public function get($ev_page_num, $page) {
        
    }
	public function index(){
		$this->display();
	}
	 public function count() {
        
    }
    /**
     * 判断登录用户类型(4s店用户 集团用户),并拼凑sql条件返回
     * @return string 4s店id或ids
     */
    public function userType(){
    	// $data = array('uid'=>10136,'group_id'=>4);
    	// $data = array('uid'=>10134,'group_id'=>5);
    	// session('user_info',json_encode($data));
    	$u_type = $this->user_info;
    	if($u_type['group_id'] == 4){
    		$map['u_id'] = array('eq',$u_type['uid']);
    		$fid = M('fours_user')->field('f_id')->where($map)->find();
    		$where['fid'] = array('eq',$fid['f_id']);
    		$id = M('fours')->field('channel_id')->where($where)->find();//4s店来源ids
    		// var_dump($id);
	    	return $id['channel_id'];
    	} elseif($u_type['group_id'] == 5){
    		$map['u_id'] = array('eq',$u_type['uid']);
    		$id = M('bloc_user')->field('b_id')->where($map)->find();//集团id
    		$where['b_id'] = array('eq',$id['b_id']);
    		$ids = M('fours')->field('channel_id')->where($where)->getField('channel_id',true);//4s店来源ids
    		$list = array('b_id'=>$id['b_id'],'channel_id'=>$ids);
    		// var_dump($list);
    		return $list;
    	}

    }
    /**
     * 获取登录用户(集团)信息
     */
    public function userInfo(){ 
        $_col = 'b.b_name,b.addr,b.contact,b.phone,bu.real_name,u.user_name';
    	$u_type = $this->user_info;
    	$uid = $this->getIds();
    	if($u_type['group_id'] == 5){
	    	$map['bu.u_id'] = array('eq',$u_type['uid']);//用户id
	    	$map['bu.b_id'] = array('eq',$uid['b_id']);//集团id
    		$u_info = M('bloc_user')
    				 ->field($_col)
    			     ->alias('bu')
    			     ->join("LEFT JOIN " . C('DB_PREFIX') . 'bloc b ON bu.b_id=b.bid')
    			     ->join("LEFT JOIN " . C('DB_PREFIX') . 'user u ON bu.u_id=u.uid')
    			     ->where($map)
    			     ->find();
    		$list = array(
    				'b_name' => $u_info['b_name'],
    				'addr' => $u_info['addr'],
    				'contact' => $u_info['contact'],
    				'mobile' => $u_info['phone'],
    				'login_name' => $u_info['user_name'],
    				'real_name' => $u_info['real_name'],
    				'type' => '集团用户'
    			);
    		// echo "<pre>";
    		// var_dump($list);     	
    		return $list;
    	} elseif($u_type['group_id'] == 4){
            $_col = 'fu.real_name,u.user_name,f.f_name,f.addr,f.contact,f.phone';
    		$map['fu.f_id'] = array('eq',$uid);//4店id
    		$map['fu.u_id'] = array('eq',$u_type['uid']);//用户id
    		$u_info = M('fours_user')
    				 ->field($_col)
    			     ->alias('fu')
    			     ->join("LEFT JOIN " . C('DB_PREFIX') . 'fours f ON fu.f_id=f.fid')
    			     ->join("LEFT JOIN " . C('DB_PREFIX') . 'user u ON fu.u_id=u.uid')
    			     ->where($map)
    			     ->find();
    		$list = array(
    				'b_name' => $u_info['f_name'],
    				'addr' => $u_info['addr'],
    				'contact' => $u_info['contact'],
    				'mobile' => $u_info['phone'],
    				'login_name' => $u_info['user_name'],
    				'real_name' => $u_info['real_name'],
    				'type' => '4s店用户'
    			);
    		// echo "<pre>";
    		// var_dump($u_info);
    		return $list;
    	}
    }
    /**
     * 更新用户信息
     * @return [type] [description]
     */
    public function updInfo($where){
    	$u_type = $this->user_info;
    	$uid = $this->userType();
    	if($u_type['group_id'] == 5){
	    	$map['u_id'] = array('eq',$u_type['uid']);//用户id
	    	$map['b_id'] = array('eq',$uid['b_id']);//集团id
	    	// var_dump($where);exit;
    		// $rec1 = M('bloc_user')->where($map)->save($where[0]);
    		$rec = M('bloc')->where('bid='.$uid['b_id'])->save($where);	
    	} else{
    		$map['channel_id'] = array('eq',$uid);//4店id
    		$rec = M('fours')->where($map)->save($where);
    	}
        return $rec;
    }
    /**
     * 修改密码
     * 
     */
    public function updPwd($where){
    	$u_type = $this->user_info;
    	//判断原密码是否正确
    	$map['uid'] = array('eq',$u_type['uid']);
    	// $map['passwd'] = array('eq',$where['oldpwd']);
    	$map['passwd'] = array('eq',md5($where['oldpwd']));
    	$rec1 = M('user')->where($map)->find();
    	// var_dump($rec1);
    	if($rec1){
    		$data = array('passwd'=>md5($where['newpwd']));
    		$rec2 = M('user')->where('uid='.$u_type['uid'])->save($data);
    		return true;
    	} else{
    		return '原密码有误';
    	}
    }
	/**
     * 拼凑属于4s店或集团的查询条件
     * @param maxed $params 
     * @return [type] [description]
     */
    public function getFourMap(){
    	$u_type = $this->user_info;
    	$id = $this->userType();
        if($u_type['group_id'] == 5){
            //如果该集团没有绑定4s店，则查不到数据
            if ($id['channel_id']) {
                $map['co.comefrom'] = array('in',$id['channel_id']);
            }else{
                $map['co.comefrom'] = array('eq',$id['channel_id']);
            }
        } elseif($u_type['group_id'] == 4){
            $map['co.comefrom'] = array('eq',$id);
        }
        // dump($map);
        return $map;
    }
    /**
     * 获取集团或4s店的渠道id
     */
    public function returnIds(){
        $u_type = $this->user_info;
        $id = $this->userType();
        if($u_type['group_id'] == 5){
            return $id['channel_id'];
        } elseif($u_type['group_id'] == 4){
            return $id;
        }
    }
    /**
     * 获取集团(名字)或4s店的id
     */
    public function returnFoursIds(){
        $u_type = $this->user_info;
        $id = $this->userType();
        if($u_type['group_id'] == 5){
            $map['u_id'] = array('eq',$u_type['uid']);
            $id = M('bloc_user')->field('b_id')->where($map)->find();//集团id
            $where['b_id'] = array('eq',$id['b_id']);
            $ids = M('fours')->field('fid')->where($where)->getField('fid',true);//4s店来源ids
            foreach($ids as $value){
                $fourN = M('fours')->where('fid='.$value)->find();
                $fourName[] = $fourN['f_name'];
            }
            // var_dump($fourName);
            return $fourName;
        } elseif($u_type['group_id'] == 4){
            $map['u_id'] = array('eq',$u_type['uid']);
            $fid = M('fours_user')->field('f_id')->where($map)->find();
            return $fid['f_id'];
        }
    }
     /**
     * 获取集团或4s店的id
     */
    public function getIds(){
        $u_type = $this->user_info;
        $id = $this->userType();
        if($u_type['group_id'] == 5){
            $map['u_id'] = array('eq',$u_type['uid']);
            $id = M('bloc_user')->field('b_id')->where($map)->find();//集团id
            return $id;
        } elseif($u_type['group_id'] == 4){
            $map['u_id'] = array('eq',$u_type['uid']);
            $fid = M('fours_user')->field('f_id')->where($map)->find();
            return $fid['f_id'];
        }
    }
    /**
     * 车辆对应来源-获取来源名称
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function carToFours($params){
        $map['from_id'] = array('eq',$params);
        $result = M('come_from')->where($map)->find();
        return $result['from_name'];
    }
    public function FoursToCar($params){
        $map['from_name'] = array('eq',$params);
        $result = M('come_from')->where($map)->find();
        return $result['from_id'];
    }
}